import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

import com.qq.spirit.image.OCR;

public class TestOCR {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = "D:\\workspace\\astdGame\\checkcode1.png";
		try {
			InputStream ins = new FileInputStream(new File(path));
//			pluszy(ins);
			String valCode = new OCR().recognizeText(ins, "png");
			System.out.println(valCode);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void pluszy(InputStream ins) {
		try {
			BufferedImage image;
			image = ImageIO.read(ins);
			int width = image.getTileWidth();
			int height = image.getTileHeight();
			Map<Integer, Integer> mapColor = new HashMap<Integer, Integer>();
			for (int i = 0; i < width; i++)
				for (int j = 0; j < height; j++) {
					int color = image.getRGB(i, j);
					Integer count = mapColor.get(color);
					if (count == null)
						count = 0;
					count++;
					mapColor.put(color, count);
				}

			List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(mapColor.entrySet());
			Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
				@Override
				public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
					return o2.getValue() - o1.getValue();
				}

			});
			System.out.println(Arrays.toString(list.toArray()));
			int intBack = list.get(0).getKey();
			Set<Integer> setColor = new HashSet<Integer>();
			for(int i=1;i<5;i++)
			{
//			for (Map.Entry<Integer, Integer> entry : list) {
				Map.Entry<Integer, Integer> entry = list.get(i);
				setColor.add(entry.getKey());
			}
//			list = list.subList(1, 5);
			for (int i = 0; i < width; i++)
				for (int j = 0; j < height; j++) {
					int color = image.getRGB(i, j);
					if (setColor.contains(color))
						continue;
					image.setRGB(i, j, intBack);
				}


			ImageIO.write(image, "png", new File("D:\\workspace\\astdGame\\checkcode1.png"));
			// ImageInputStream ios = ImageIO.createImageInputStream(image);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}